package com.x.xiaoshuo.widget.readview.aim;

import android.graphics.Bitmap;
import android.graphics.Canvas;

import com.x.xiaoshuo.manager.SettingManager;


public class AimManager implements AimInterface {
    public static final int AIM_TYPE_NO = 1;
    public static final int AIM_TYPE_OVERLAP = 2;
    public static final int AIM_TYPE_AUTO = 3;
    public static final int AIM_TYPE_SIMULATION = 0;
    AimInterface aim;

    AimInterface simulation;
    AimInterface overlap;
    AimInterface noAim;
    AimInterface autoAim;
    ReadView readView;

    public AimManager(ReadView readView) {
        simulation = new SimulationAim(readView);
        overlap = new OverlappedAim(readView);
        noAim = new SlideAim(readView);
        autoAim = new AutoReadAim(readView);
        aim = simulation;
        this.readView = readView;
    }

    public boolean isAutoRead() {
        if (aim instanceof AutoReadAim) {
            return true;
        }
        return false;
    }

    public void pauseAim() {
        if (aim instanceof AutoReadAim) {
            ((AutoReadAim) aim).pauseAnimation();
        }
    }

    public void setAim(int type) {
        aim.abortAnimation();
        switch (type) {
            case AIM_TYPE_NO:
                aim = noAim;
                break;
            case AIM_TYPE_OVERLAP:
                aim = overlap;
                break;
            case AIM_TYPE_SIMULATION:
                aim = simulation;
                break;
            case AIM_TYPE_AUTO:
                aim = autoAim;
                aim.startAnimation();
                break;
        }
    }

    @Override
    public void drawNextPageAreaAndShadow(Canvas canvas) {
        aim.drawNextPageAreaAndShadow(canvas);
    }

    @Override
    public void drawCurrentPageShadow(Canvas canvas) {
        aim.drawCurrentPageShadow(canvas);
    }

    @Override
    public void drawCurrentBackArea(Canvas canvas) {
        aim.drawCurrentBackArea(canvas);
    }

    @Override
    public void drawCurrentPageArea(Canvas canvas) {
        aim.drawCurrentPageArea(canvas);
    }

    @Override
    public void calcPoints() {
        aim.calcPoints();
    }

    @Override
    public void calcCornerXY(float x, float y) {
        aim.calcCornerXY(x, y);
    }

    @Override
    public void startAnimation() {
        aim.startAnimation();
        if (aim instanceof NoAim) {
            ((NoAim) aim).startAnimation(0);
        } else {
            aim.startAnimation();
        }
    }

    @Override
    public void abortAnimation() {
        aim.abortAnimation();
        if (aim instanceof AutoReadAim) {
            setAim(SettingManager.getInstance().getFlipStyle());
            readView.postInvalidate();
        }
    }

    @Override
    public void restoreAnimation() {
        aim.restoreAnimation();
    }

    @Override
    public void setBitmaps(Bitmap mCurPageBitmap, Bitmap mNextPageBitmap) {
        aim.setBitmaps(mCurPageBitmap, mNextPageBitmap);
    }

    @Override
    public void setTheme(int theme) {
        aim.setTheme(theme);
    }

    @Override
    public void computeScroll() {
        aim.computeScroll();
    }

    @Override
    public void jumpToChapter(int chapter) {
        aim.jumpToChapter(chapter);
    }

    @Override
    public void destory() {
        simulation.destory();
        overlap.destory();
        noAim.destory();
        autoAim.destory();
    }
}
